package com.romotc.qqrobot.webqq.qqInterfaceLog;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.romotc.joyin.bean.Customer;
import com.romotc.joyin.bean.CustomerService;
import com.romotc.joyin.bean.MemberCustService;
import com.romotc.qqrobot.bean.MemberCustServiceService;
import com.romotc.qqrobot.bean.PollResult;
import com.romotc.qqrobot.bean.QQLogedin;
import com.romotc.qqrobot.bean.QQUserFriend;
import com.romotc.qqrobot.bean.QQUserFriendService;
import com.romotc.qqrobot.bean.RobotUser;
import com.romotc.qqrobot.bean.RobotUserService;
import com.romotc.qqrobot.bean.SendMsg;
import com.romotc.qqrobot.bean.SendMsgService;
import com.romotc.qqrobot.config.bean.QQUrlWatcherConfig;
import com.romotc.qqrobot.webqq.WebQQConstant;
import com.romotc.qqrobot.webqq.bean.Poll2_ResponseBody;
import com.romotc.qqrobot.webqq.bean.Poll2_ResponseBody_Result;
import com.romotc.scache.RequestSpy;
/**
 * 保存客户的消息到数据库 以便虚拟客服读取
 * @author Administrator
 *
 */
public class Poll2_Save2DB_Log extends Poll2_Processor_Log {

	/**
	 * 处理所有类型的消息
	 */
	@Override
	String getPollType() {
		return Poll2_Processor_Log.Key_AllType;
	}

	@Override
	void processPollEmpty(RequestSpy rqSpy, QQUrlWatcherConfig config,
			Poll2_ResponseBody response) {
		PollResult result = new PollResult(response);
		result.Save();
	}

	@Override
	void processPollResult(RequestSpy rqSpy, QQUrlWatcherConfig config,
			Poll2_ResponseBody_Result result, Poll2_ResponseBody response,
			int resultIdx) {
		QQLogedin curQQ = QQLogedin.getCurQqLogedIn(rqSpy);
		PollResult resultBean = new PollResult(result);
		if(resultBean.getTo_uin() == 0) {//102的没有消息内容不需要转发
			resultBean.setTo_uin(Long.valueOf(curQQ.getUin()));
			resultBean.Save();
			return;
		}
		
		//查询发送消息的好友对应的客户  客服人员信息
		//uin 和接受消息的QQ 查询qq_user_friends表 查询好友信息
		QQUserFriend friend = QQUserFriendService.qryQFriendByUin(QQLogedin.getCurQqLogedIn(rqSpy).getId(),
				resultBean.getFrom_uin());
		
		//机器人用户id
		long robotUid = RobotUserService.qryUidByQQNum(resultBean.getTo_uin());
		//好友对应的客户
		Customer cust = CustomerService.qryCustomerByName(friend.getMarkname(), robotUid);
		
		//客户对应的客服
		MemberCustService memCustService = null; 
		if(cust != null && cust.getCsId() > 0) {
			memCustService = MemberCustServiceService.qryMemberCustServiceById(cust.getCsId());
		}
		
		if(cust != null)
			resultBean.setCustId(cust.getId());
		
		//如果没有查询到客户信息 设置下custid为好友uin的负数
		if(resultBean.getCustId() <= 0) {
			resultBean.setCustId(-resultBean.getFrom_uin());
		}
		
		//设置默认客服为客户数量最少的客服人员
		if(memCustService == null) {
			memCustService = MemberCustServiceService.qryMinCustsService(robotUid);
		}
		
		//RomUnDo 设置的客服人员应该在线才行
		resultBean.setCustServiceId(memCustService.getId());
		
//		if(friend != null) {
//			//如果是好友就好办了
//			//设置客户id
//			//1.如果好友表里面有custId 则直接设置客户id 
//			if(friend.getCustId() > 0) {
//				resultBean.setCustId(friend.getCustId());
//			}else if(StringUtils.isNotBlank(friend.getMarkname())){
//				//2.如果没有则根据markName查询客户Id
//				
//				if(cust != null) {
//					resultBean.setCustId(cust.getId());
//				}
//				//RomUnDo 非好友暂时不处理 以后增加对临时会话 加好友的逻辑处理
//				
//			}else {
//				//3.没有markName的情况暂时不做处理(非客户的情况应该在登录时或者添加好友拦截器中进行处理)
//				//RomUnDo
//			}
//		}
		
		resultBean.Save();
		
		
		//临时代码
		List<SendMsg> sendList = SendMsgService.qryMySendTask(QQLogedin.getCurQqLogedIn(rqSpy));
		SendMsgService.sendMsg(sendList);
	}

	/**
	 * 客服虚拟账号不记录消息
	 */
	@Override
	public boolean checkSkipLog(RequestSpy rqSpy, QQUrlWatcherConfig config) {
		
		QQLogedin curQQ = QQLogedin.getCurQqLogedIn(rqSpy);
		if(curQQ == null)//异常情况不处理
			return true;
		
		//因为客服虚拟账号的poll本身就是从数据库中取得 这里不记录客服虚拟账号的消息记录 
		if(WebQQConstant.isRobotUin(curQQ.getLoginUserName())) {
			return true;
		}
		return false;
	}

}
